import Vue from 'vue'
import Router from 'vue-router'
import Index from './views/Index.vue'
import Login from './views/Login.vue'
import Carlist from './views/Carlist.vue'
import CarlistCtt from './views/CarlistCtt.vue'
import Person from './views/Person.vue'
import PersonOrder from './views/PersonOrder.vue'
import UpdHeadPic from './views/UpdHeadPic.vue'
import SearchOrder from './views/PersonSearchOrder.vue'
import PersonIngOrder from './views/PersonIngOrder.vue'
import PersonOverOrder from './views/PersonOverOrder.vue'
import PersonCarSharingOrder from './views/PersonCarSharingOrder.vue'
import PersonInfo from './views/PersonInfo.vue'
import PersonCollect from './views/PersonCollect.vue'
import PersonMessage from './views/PersonMessage.vue'
import PersonUpdPwd from './views/PersonUpdPwd.vue'
import PersonIdCard from './views/PersonIdCard.vue'
import Cardetail from './views/Cardetail.vue'
import Activity from './views/Activity.vue'
import ActivityDetail from './views/ActivityDetail.vue'
import Register from './views/Register.vue'
import Home from './views/Home.vue'
import Order from './views/Order.vue'
import TopCollect from './views/TopCollect.vue'
import TopSale from './views/TopSale.vue'
Vue.use(Router)

let router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  scrollBehavior(to, from, savePosition) { // 在点击浏览器的“前进/后退”，或者切换导航的时候触发。
    // console.log(to) // to：要进入的目标路由对象，到哪里去
    // console.log(from) // from：离开的路由对象，哪里来
    // console.log(savePosition) // savePosition：会记录滚动条的坐标，点击前进/后退的时候记录值{x:?,y:?}
    if (savePosition) {
      return savePosition;
    } else {
      return { x: 0, y: 0 }
    }
  },
  routes: [
    {
      path: '/',
      redirect: '/index/home' //路由跳转
    },
    {
      path: '/index',
      name: 'index',
      redirect: '/index/home',
      component: Index,
      children: [
        {
          path: 'home',
          name: 'home',
          component: Home,
          children: [
            {
              path: '/index/home',
              name: 'TopCollect',
              component: TopCollect,
              children: [
                {
                  path: '/index/home',
                  name: 'TopSale',
                  component: TopSale,
                },
              ]
            },
          ]
        },
        {
          path: 'carlist',
          name: 'carlist',
          component: Carlist,
          children: [
            {
              path: '/CarlistCtt',
              name: 'CarlistCtt',
              component: CarlistCtt
            },
          ], redirect: '/CarlistCtt'
        },
        {
          path: 'person',
          name: 'person',
          component: Person,
          children: [
            {
              path: '/person/UpdHeadPic',
              name: 'UpdHeadPic',
              component: UpdHeadPic,
              meta: { title: '/person/UpdHeadPic' }
            },
            {
              path: '/person/SearchOrder',
              name: 'SearchOrder',
              component: SearchOrder,
            },
            {
              path: '/person/PersonOrder',
              name: 'PersonOrder',
              component: PersonOrder,
              meta: { title: '全部订单' }
            },
            {
              path: '/person/PersonIngOrder',
              name: 'PersonIngOrder',
              component: PersonIngOrder,
              meta: { title: '已完成/正在进行' }
            },
            {
              path: '/person/PersonOverOrder',
              name: 'PersonOverOrder',
              component: PersonOverOrder
            },
            {
              path: '/person/PersonCarSharingOrder',
              name: 'PersonCarSharingOrder',
              component: PersonCarSharingOrder,
              meta: { title: '拼车订单' }
            },
            {
              path: '/person/PersonInfo',
              name: 'PersonInfo',
              component: PersonInfo,
              meta: { title: '用户信息' }
            },
            {
              path: '/person/PersonCollect',
              name: 'PersonCollect',
              component: PersonCollect,
              meta: { title: '我的收藏' }
            },
            {
              path: '/person/PersonMessage',
              name: 'PersonMessage',
              component: PersonMessage,
              meta: { title: '我的评价' }
            },
            {
              path: '/person/PersonUpdPwd',
              name: 'PersonUpdPwd',
              component: PersonUpdPwd,
              meta: { title: '修改密码' }
            },
            {
              path: '/person/PersonIdCard',
              name: 'PersonIdCard',
              component: PersonIdCard,
              meta: { title: '身份证认证' }
            },
          ], redirect: '/person/PersonOrder'
        },
        {
          path: '/cardetail',
          name: 'cardetail',
          component: Cardetail
        },
        {
          path: 'order',
          name: 'order',
          component: Order,
        },
        {
          path: 'activity',
          name: 'activity',
          component: Activity
        },
        {
          path: 'ActivityDetail',
          name: 'ActivityDetail',
          component: ActivityDetail
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      component: Register
    },

  ]
})

//设置路由守卫
router.beforeEach((to, from, next) => {
  //进入个人中心必须先登录
  //判断有无本地存储
  let tokenIsExists = localStorage.getItem('mytoken') ? true : false
  if ((to.path == '/index/person' || to.path == '/index/order' || to.path == '/index/pay' || to.path == '/index/orderPay') && !tokenIsExists) {
    next('/login')
    //未登录，跳转到登录组件
  } else {
    if (tokenIsExists) {
      next()
      //已经登陆过的并且取得token，允许访问路由
    } else {
      next() //允许访问路由
    }
  }
})

export default router



